--
-- Author: shunguo.chen
-- Date: 2019-05-24 10:45:48
--
-- SkillTalentPanel 技能天赋panel
--

local ResRender = require("app.widget.renders.ResRender")
local TalentRender = import(".renders.TalentRender")

local ClassRef = app.mvc.createPanelClass(...)

local TALENT_MAX_NUM = 12

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    -- top
    self._resRender = ResRender.create(self._ui.Panel_point)
    self._resRender:setData({type=Enums.TipsType.daibi, id=Enums.lootType.talentPoint, num=0})

    -- left talents
    local leftPanel = self._ui.Panel_left
    self._specNameText = leftPanel:getChildByName("title")
    self._specTipText = leftPanel:getChildByName("tips")
    self._resetBtn = leftPanel:getChildByName("Button")
    self._resetBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "talent_reset")
    end)

    local linePanel = leftPanel:getChildByName("Panel_sens")
    local talentRenders = {}
    local render
    for i=1,TALENT_MAX_NUM do
        render = TalentRender.create(leftPanel:getChildByName("talent_" .. i))
        render:onBtnClick(handler(self, self._setCurRender))
        talentRenders[i] = render
    end
    self._hLine = linePanel:getChildByName("line_1_2")
    self._vLine = linePanel:getChildByName("line_1_4")
    self._talentRenders = talentRenders
    self._linePanel = linePanel

    -- right talent detail
    local rightPanel = self._ui.Panel_tips
    self._talentIcon = rightPanel:getChildByName("icon")
    self._talentNameText = rightPanel:getChildByName("title")
    self._talentDescText = rightPanel:getChildByName("Text")
    self._needActiveTip = rightPanel:getChildByName("tips")
    self._alreadyActiveTip = rightPanel:getChildByName("tips_1")
    self._activeBtn = rightPanel:getChildByName("Button")
    self._activeBtn:addClickEventListener(function()
        if UD:getTalentPoint() > 0 then
            executeFunc(self._onOperate, "talent_active", self._curTalent)
            self:_playAction()
        else
            display.pushToast(L("lua_code_text_85"))
        end
    end)
end

function ClassRef:getGuideNode(id)
    if id == 109 then
        -- 天赋提示2，查看天赋效果
        return self._talentIcon
    elseif id == 110 then
        -- 天赋提示3，点击激活天赋按钮
        return self._activeBtn
    elseif id == 111 then
        -- 天赋提示4，查看天赋点提示
        return self._resRender
    end
end

-- 刷新ResRender
function ClassRef:refreshResNum()
    self._resRender:refreshNum()
end

-- 激活talent
function ClassRef:updateTalent(upTalent)
    self:refreshResNum()

    local upSeat = upTalent:getSeat()
    for i,talent in ipairs(self._talents) do
        local seat = talent:getSeat()
        if talent:getPreSeat() == upSeat then
            self:_showLine(upSeat, seat, true)
            self._talentRenders[seat]:setData(talent)
        end
    end

    if upTalent:getId() == self._curTalent:getId() then
        self._curRender:setData(upTalent)
        self:_setCurRender(self._curRender)
    end
end

-- specType SpecType 专精
-- talents {Talent, ...} 该专精的所有天赋
function ClassRef:refresh(specType, talents)
    self:refreshResNum()

    self._specType = specType
    self._talents = talents

    -- specType
    self._specNameText:setString(specType:getNameDes())
    self._specTipText:setString(specType:isActive() and L("spec_state_1") or L("spec_state_2"))

    -- talents
    for i,render in ipairs(self._talentRenders) do
        render:setVisible(false)
    end
    local lines = self._linePanel:getChildren()
    for i,line in ipairs(lines) do
        line:setVisible(false)
    end

    local seat, preSeat, render
    local minSeat, minWaitActiveSeat = 100, 100
    for i,talent in ipairs(talents) do
        seat = talent:getSeat()
        preSeat = talent:getPreSeat()
        render = self._talentRenders[seat]
        render:setData(talent)
        render:setVisible(true)

        -- 优先选择可以激活最小位置的，然后选择没有前置条件最小位置的
        if not talent:isActive() and talent:isPreActive() then
            minWaitActiveSeat = math.min(seat, minWaitActiveSeat)
        end
        if preSeat == 0 then
            minSeat = math.min(seat, minSeat)
        end

        if preSeat > 0 then
            self:_showLine(preSeat, seat, talent:isPreActive())
        end
    end

    local selectSeat
    if minSeat < 100 then
        selectSeat = minSeat
    end
    if minWaitActiveSeat < 100 then
        selectSeat = minWaitActiveSeat
    end

    self:_setCurRender(self._talentRenders[selectSeat])
end

function ClassRef:_showLine(from, to, isLight)
    if from > to then
        from, to = to, from
    end

    local lineName = string.format("line_%s_%s", from, to)
    local line = self._linePanel:getChildByName(lineName)
    if not line then
        if (to - from) == 1 then
            line = self._hLine:clone()
        else
            line = self._vLine:clone()
        end
        line:setName(lineName)
        line:setPosition(self._talentRenders[from]:getPosition())
        self._linePanel:addChild(line)
    end
    line:setVisible(true)

    local bar = line:getChildByName("bar")
    bar:setPercent(isLight and 100 or 0)
end

function ClassRef:_setCurRender(render)
    if not render then return end

    if self._curRender then
        self._curRender:setSelected(false)
    end
    render:setSelected(true)

    self._curRender = render
    self._curTalent = render:getData()

    self:_refreshTalent()
end

function ClassRef:_refreshTalent()
    local talent = self._curTalent

    self._talentIcon:loadTexture(talent:getIcon())
    self._talentNameText:setString(talent:getName())
    self._talentDescText:setString(talent:getDesc())

    local isActive = talent:isActive()
    local canActive = talent:canActive()
    self._alreadyActiveTip:setVisible(isActive)
    self._activeBtn:setVisible(canActive)
    self._needActiveTip:setVisible(not isActive and not canActive)
    if not isActive and not canActive then
        local tipStr
        if not talent:isPreActive() then
            tipStr = L("ui_csd_289")
        else
            local minLv = talent:getMinLv()
            tipStr = string.format(L("task_road_desc11"), minLv)
        end
        self._needActiveTip:setString(tipStr)
    end
end

function ClassRef:_playAction()
    local actionNode = self._actionNode
    local actionTimeline = self._actionTimeline
    if not actionNode then
        actionNode = cc.CSLoader:createNode(Res.upgradeNode,false,false)
        actionTimeline = actionNode:getActionByTag(0)
        actionTimeline:setLastFrameCallFunc(function()
            actionNode:setVisible(false)
        end)
        self._ui.Panel_left:addChild(actionNode)
    end

    actionNode:setPosition(self._curRender:getPosition())
    actionTimeline:gotoFrameAndPlay(0, false)
end

return ClassRef
